package com.dog.basics.service.common.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.ArticleCommentAddDto;
import com.dog.basics.bean.dto.admin.common.ArticleCommentEditDto;
import com.dog.basics.bean.dto.admin.common.ArticleCommentFormDto;
import com.dog.basics.bean.model.common.DogComArticle;
import com.dog.basics.bean.model.common.DogComArticleComment;
import com.dog.basics.bean.model.common.DogComArticleUser;
import com.dog.basics.bean.model.common.DogUser;
import com.dog.basics.bean.vo.admin.common.PcArticleCommentVo;
import com.dog.basics.bean.vo.api.common.ArticleCommentVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.DogComArticleCommentMapper;
import com.dog.basics.dao.common.DogComArticleMapper;
import com.dog.basics.dao.common.DogComArticleUserMapper;
import com.dog.basics.dao.common.DogUserMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.IDogComArticleCommentService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * CMS评论表 服务实现类
 * </p>
 *
 * @author guonh
 * @since 2020-05-23
 */
@Service
@AllArgsConstructor
public class DogComArticleCommentServiceImpl extends ServiceImpl<DogComArticleCommentMapper, DogComArticleComment> implements IDogComArticleCommentService {
    private final DogUserMapper dogUserMapper;
    private final DogComArticleMapper dogComArticleMapper;
    private final DogComArticleUserMapper articleUserMapper;


    /**
     * @Description: 增加cms文章评论
     * @Param:
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/22  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObj(ArticleCommentAddDto obj) {
        DogComArticleComment dogComArticleComment = new DogComArticleComment();
        BeanUtil.copyProperties(obj, dogComArticleComment);
        dogComArticleComment.setCreateTime(DateUtil.currentSeconds());
        dogComArticleComment.setCreateBy(SecurityUtils.getSysUser().getUserId());
        Validator.validateTrue(this.save(dogComArticleComment), " 增加cms文章评论失败!");
        DogComArticle dogComArticle= dogComArticleMapper.selectById(obj.getArticleId());
        dogComArticle.setCommentCount(dogComArticle.getCommentCount()+1);
        dogComArticle.setLastCommentTime(DateUtil.currentSeconds());
        Validator.validateTrue(retBool(dogComArticleMapper.updateById(dogComArticle)), " 增加cms文章评论数失败!");
        return Boolean.TRUE;
    }


    /**
     * @Description: 删除cms文章评论
     * @Param: ids
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/22  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        DogComArticleComment dogComArticleComment = this.getById(id);
        Validator.validateNotNull(dogComArticleComment, "无此数据");
        return this.removeById(id);
    }


    /**
     * @Description: 修改cms文章评论
     * @Param:
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/22  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editObj(ArticleCommentEditDto obj) {
        DogComArticleComment dogComArticleComment = this.getById(obj.getId());
        Validator.validateNotNull(dogComArticleComment, "无此数据");
        BeanUtil.copyProperties(obj, dogComArticleComment);
        dogComArticleComment.setUpdateTime(DateUtil.currentSeconds());
        dogComArticleComment.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        return this.updateById(dogComArticleComment);
    }

    /**
     * @Description: 分页cms文章评论列表
     * @Param: status
     * @return: IPage<PcArticleCommentVo>
     * @Author: zhangmr
     * @Date:
     */
    @Override
    public IPage<PcArticleCommentVo> getPageList(PageParam page, ArticleCommentFormDto obj) {
        IPage iPage = this.page(page, Wrappers.<DogComArticleComment>lambdaQuery()
                .eq(ObjectUtil.isNotNull(obj.getId()), DogComArticleComment::getId, obj.getId())
                .eq(ObjectUtil.isNotNull(obj.getUserId()), DogComArticleComment::getUserId, obj.getUserId())
                .eq(ObjectUtil.isNotNull(obj.getArticleId()), DogComArticleComment::getArticleId, obj.getArticleId())
                .orderBy(Boolean.TRUE, obj.getIsOrderByAvg(), DogComArticleComment::getCreateTime));
        List<DogComArticleComment> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(e -> {
            PcArticleCommentVo pcArticleCommentVo = new PcArticleCommentVo();
            BeanUtil.copyProperties(e, pcArticleCommentVo);
            DogUser dogUser =dogUserMapper.selectById(e.getUserId());
            pcArticleCommentVo.setUserName(ObjectUtil.isNotNull(dogUser)?dogUser.getNickName():"");
            return pcArticleCommentVo;
        }).collect(Collectors.toList()));
        return iPage;
    }


    /**
     * @Description: 增加cms文章评论
     * @Param:
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/22  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObjApp(ArticleCommentAddDto obj) {
        DogComArticleComment dogComArticleComment = new DogComArticleComment();
        BeanUtil.copyProperties(obj, dogComArticleComment);
        dogComArticleComment.setCreateTime(DateUtil.currentSeconds());
        dogComArticleComment.setCreateBy(SecurityUtils.getUser().getUserId());
        dogComArticleComment.setCommentTime(DateUtil.currentSeconds());
        dogComArticleComment.setUserId(SecurityUtils.getUser().getUserId());
        Validator.validateTrue(this.save(dogComArticleComment), " 增加cms文章评论失败!");
        DogComArticle dogComArticle = dogComArticleMapper.selectById(obj.getArticleId());
        dogComArticle.setCommentCount(dogComArticle.getCommentCount()+1);
        dogComArticle.setLastCommentTime(DateUtil.currentSeconds());
        Validator.validateTrue(retBool(dogComArticleMapper.updateById(dogComArticle)), " 增加cms文章评论数失败!");
        return Boolean.TRUE;
    }

    /**
     * @Description: 增加cms文章评论点赞数量
     * @Param:
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/22  0027
     */
    @Override
    public Boolean addedit(Long id) {

        DogComArticleComment articleComment = this.getById(id);
        Integer count = articleUserMapper.selectCount(Wrappers.<DogComArticleUser>lambdaQuery()
                .eq(DogComArticleUser::getAorcId, id)
                .eq(DogComArticleUser::getUserId, SecurityUtils.getUser().getUserId())
                .eq(DogComArticleUser::getOperationFlag, 2));
        Validator.validateTrue(count.equals(0),"不能重复增加");

        Validator.validateNotNull(articleComment, "无此数据");

        articleComment.setUp(articleComment.getUp()+1);
        DogComArticleUser articleUser = new DogComArticleUser();
        articleUser.setAorcId(id);
        articleUser.setOperationFlag(2);
        articleUser.setUserId(SecurityUtils.getUser().getUserId());
        articleUser.setCreateTime(DateUtil.currentSeconds());
        articleUser.setCreateBy(SecurityUtils.getUser().getUserId());
        Validator.validateTrue(retBool(articleUserMapper.insert(articleUser)),"关联信息添加失败");
        return this.updateById(articleComment);
    }

    /**
     * @Description: api分页cms文章评论列表
     * @Param: status
     * @return: IPage<PcArticleCommentVo>
     * @Author: zhangmr
     * @Date:
     */
    @Override
    public IPage<ArticleCommentVo> getApiPageList(PageParam page, ArticleCommentFormDto obj) {
        IPage iPage = this.page(page, Wrappers.<DogComArticleComment>lambdaQuery()
                .eq(ObjectUtil.isNotNull(obj.getId()), DogComArticleComment::getId, obj.getId())
                .eq(ObjectUtil.isNotNull(obj.getUserId()), DogComArticleComment::getUserId, obj.getUserId())
                .eq(ObjectUtil.isNotNull(obj.getArticleId()), DogComArticleComment::getArticleId, obj.getArticleId())
                .orderBy(Boolean.TRUE, obj.getIsOrderByAvg(), DogComArticleComment::getCreateTime));
        List<DogComArticleComment> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(e -> {
            ArticleCommentVo vo = new ArticleCommentVo();
            BeanUtil.copyProperties(e, vo);
            DogUser dogUser =dogUserMapper.selectById(e.getUserId());
            vo.setUserName(ObjectUtil.isNotNull(dogUser)?dogUser.getNickName():"");
            vo.setUserPic(ObjectUtil.isNotNull(dogUser)?dogUser.getPic():"");
            return vo;
        }).collect(Collectors.toList()));
        return iPage;
    }
}
